<template>
  <div class="enterpriseRanking">
    <div class="form">
      <el-form model="form" inline>
        <el-form-item label="行业名字">
          <el-input v-model="form.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="add">
      <el-button type="primary" @click="addEnterpriseToIndustry">添加企业到行业</el-button>
      <el-button type="primary" @click="addIndustry">添加行业</el-button>
    </div>
    <div class="table">
      <el-table style="height: calc(100vh - 230px)" :data="tableData">
        <el-table-column type="index" width="60" label="序号"></el-table-column>
        <el-table-column
          prop="name"
          align="center"
          show-overflow-tooltip
          label="行业名称"
        ></el-table-column>
        <el-table-column prop="description" align="center" show-overflow-tooltip label="行业描述">
          <template #default="scope">
            {{ scope.row.description ? scope.row.description : '暂无描述' }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="enterprises" label="企业数量">
          <template #default="scope">
            {{ scope.row.enterprises ? scope.row.enterprises.length : 0 }}
            <el-button type="text" size="small" @click="handleEnterprise(scope.row)">
              查看
            </el-button>
          </template>
        </el-table-column>
        <el-table-column prop="operation" label="操作">
          <template #default="scope">
            <el-button type="text" @click="editIndustry(scope.row)">编辑</el-button>
            <el-button type="text" @click="deleteIndustry(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div v-if="tableData.length" class="pagination-w">
        <el-pagination
          :total="searchParams.total"
          background
          :locale="zhCn"
          :current-page="searchParams.current"
          :page-size="searchParams.pageSize"
          :page-sizes="searchParams.pageSizes"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
    <LookEnterpriseDialog ref="lookEnterpriseDialog" @submit="getIndustryData" />
    <AddIndustryDialog ref="addEnterpriseDialog" @submit="getIndustryData" />
    <AddEnterpriseToIndustryDialog ref="addEnterpriseToIndustryDialog" @submit="getIndustryData" />
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue'
import { getIndustryPage, deleteIndustryApi } from '@/api/enterprise'
import LookEnterpriseDialog from './lookEnterpriseView.vue'
import AddIndustryDialog from './addIndustryView.vue'
import AddEnterpriseToIndustryDialog from './addEnterpriseToIndustryView.vue'
import { ElMessage } from 'element-plus'

const form = reactive({
  name: '',
})
const lookEnterpriseDialog = ref(null)
const addEnterpriseDialog = ref(null)
const enterpriseList = ref([])
const tableData = ref([])
const searchParams = ref({
  current: 1,
  pageSize: 10,
  pageSizes: [10, 20, 50, 100, 200],
  total: 0,
})
const search = async () => {
  const res = await getIndustryPage(
    form.name,
    searchParams.value.current,
    searchParams.value.pageSize
  )

  if (res.status === 200 || res.status === 201) {
    tableData.value = res.data.data.industries
    enterpriseList.value = res.data.data.enterprise
    searchParams.value.total = parseInt(res.data.data.totalCount) || 0
  }
}
const addEnterpriseToIndustryDialog = ref(null)

const editIndustry = (row) => {
  addEnterpriseDialog.value?.openDialog(row)
}

const addIndustry = () => {
  addEnterpriseDialog.value?.openDialog()
}
const handleEnterprise = (row) => {
  lookEnterpriseDialog.value?.openDialog(row)
}
const getIndustryData = async () => {
  const res = await getIndustryPage(
    form.name,
    searchParams.value.current,
    searchParams.value.pageSize
  )

  if (res.status === 200 || res.status === 201) {
    tableData.value = res.data.data.industries
    enterpriseList.value = res.data.data.enterprise
    searchParams.value.total = parseInt(res.data.data.totalCount) || 0
  }
}
const handleSizeChange = (newSize) => {
  console.log('size change', newSize)
  searchParams.value.pageSize = newSize
  getIndustryData()
}

const deleteIndustry = async (row) => {
  const res = await deleteIndustryApi(row.id)
  if (res.status === 200 || res.status === 201) {
    ElMessage.success('删除成功')
    getIndustryData()
  } else {
    ElMessage.error(res.data.data || '系统繁忙，请稍后再试')
  }
}

const addEnterpriseToIndustry = () => {
  addEnterpriseToIndustryDialog.value?.openDialog()
}
const handlePageChange = (newPage) => {
  console.log('page change', newPage)
  searchParams.value.current = newPage
  getIndustryData()
}
onMounted(() => {
  getIndustryData()
})
</script>

<style scoped lang="scss">
.enterpriseRanking {
  padding: 30px;
  position: relative;
  .add {
    position: absolute;
    top: 30px;
    right: 30px;
  }
  .table {
    .pagination-w {
      display: flex;
      justify-content: right;
      margin-top: 20px;
    }
  }
}
</style>
